package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessageEventTypes;
import com.tobeface.wechat.message.WeChatMessagePayloads;

/**
 * @author loudyn
 */
public abstract class LocationEventMessageHandlerSupport extends EventMessageHandlerSupport {

    @Override
    protected final boolean isEventMessageSupport(WeChatMessage message, String event) {
        if (!Objects.equal(WeChatMessageEventTypes.LOCATION, event)) {
            return false;
        }

        String lat = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_LATITUDE_PAYLOAD);
        String lon = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_LONGITUDE_PAYLOAD);
        String precision = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_PRECISION_PAYLOAD);
        return isLocationEventMessageSupport(message, lat, lon, precision);
    }

    /**
     * @param message
     * @param lat
     * @param lon
     * @param precision
     * @return
     */
    protected boolean isLocationEventMessageSupport(WeChatMessage message, String lat, String lon, String precision) {
        return true;
    }

    @Override
    protected final void onEventMessageReceived(WeChatMessageChannel channel, WeChatMessage message, String event) {
        String lat = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_LATITUDE_PAYLOAD);
        String lon = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_LONGITUDE_PAYLOAD);
        String precision = (String) message.getPayloads().get(WeChatMessagePayloads.EVENT_PRECISION_PAYLOAD);
        onLocationEventMessageReceived(channel, message, lat, lon, precision);
    }

    /**
     * @param channel
     * @param message
     * @param lat
     * @param lon
     * @param precision
     */
    protected abstract void onLocationEventMessageReceived(WeChatMessageChannel channel, WeChatMessage message,
                                                           String lat, String lon, String precision);


}
